package karileet.meeting.profile.fragment;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import karileet.meeting.profile.MeetingProfile;
import karileet.meeting.profile.ProfileConf;
import karileet.meeting.profile.list.adapter.ListItemAdapter;
import karileet.meeting.profile.list.adapter.MergeAdapter;
import karileet.meeting.profile.list.adapter.ScheduleListItemAdapter;
import karileet.meeting.profile.list.holder.ListItemHolder;
import karileet.meeting.profile.list.item.ListItem;
import karileet.meeting.profile.list.item.ScheduleListItem;


import karileet.meeting.profile.R;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainListFragment extends ListFragment {

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		MergeAdapter complexList = new MergeAdapter();
		ArrayAdapter<ListItem> listAdapter = new ListItemAdapter(getActivity(),
				populateMainList());
		ArrayAdapter<ScheduleListItem> scheduleAdapter = new ScheduleListItemAdapter(
				getActivity(), populateScheduleList());
		complexList.addAdapter(listAdapter);
		complexList.addAdapter(scheduleAdapter);
		setListAdapter(complexList);

	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		if (position > 3) {
			this.scheduleListClickHandler(v, position);
		} else {
			this.mainListClickHandler(v, position);
		}
	}

	private void mainListClickHandler(View v, int position) {
		MeetingProfile mp = new MeetingProfile(getActivity());
		ListItemHolder holder = (ListItemHolder) v.getTag();
		ListItem item = holder.getTag();
		FragmentManager manager = getFragmentManager();
		item.toggle();
		switch (position) {
		case 0:
			if (item.isSelected()) {
				Log.d(ProfileConf.APP, "Enabling profile");
				mp.enable();
			} else {
				Log.d(ProfileConf.APP, "Disabling profile");
				mp.disable();
			}
			mp.updateWidget();
			break;
		case 1:
			Log.d(ProfileConf.APP, "Toggling vibrate: " + item.isSelected());
			ProfileConf pc = new ProfileConf(getActivity());
			pc.setVibrateEnabled(item.isSelected());
			mp.update();
			break;
		case 2:
			Log.d(ProfileConf.APP, "Showing volume dialog");
			VolumeFragment volumeDialog = new VolumeFragment();
			volumeDialog.show(manager, Integer.toString(position));
			break;
		case 3:
			Log.d(ProfileConf.APP, "Opening market");
			Intent browserIntent = new Intent(
					Intent.ACTION_VIEW,
					Uri.parse("https://play.google.com/store/apps/details?id=karileet.meeting.profile"));
			startActivity(browserIntent);
			break;
		default:
			break;
		}
		holder.setDescriptionText(item.getDescription());
		holder.setTitleText(item.getTitle());
	}

	private void scheduleListClickHandler(View v, int position) {
		FragmentManager manager = getFragmentManager();
		switch (position) {
		case 4:
			Log.d(ProfileConf.APP, "Showing schedule start dialog");
			StartDialog startDialog = new StartDialog();
			startDialog.show(manager, Integer.toString(position));
			break;
		case 5:
			Log.d(ProfileConf.APP, "Showing schedule stop dialog");
			StopDialog stopDialog = new StopDialog();
			stopDialog.show(manager, Integer.toString(position));
			break;
		default:
			break;
		}
	}

	private List<ListItem> populateMainList() {
		Resources res = getResources();
		String[] titles = res.getStringArray(R.array.listTitlesDefault);
		String[] titlesAlt = res.getStringArray(R.array.listTitlesActive);
		String[] descr = res.getStringArray(R.array.listDescriptionsDefault);
		String[] descrAlt = res.getStringArray(R.array.listDescriptionsActive);
		ProfileConf ps = new ProfileConf(getActivity().getApplicationContext());
		List<ListItem> list = new ArrayList<ListItem>();
		list.add(new ListItem(titles[0], descr[0], titlesAlt[0], descrAlt[0],
				ps.isProfileEnabled()));
		list.add(new ListItem(titles[1], descr[1], titlesAlt[1], descrAlt[1],
				ps.isVibrateEnabled()));
		list.add(new ListItem(titles[2], descr[2], titlesAlt[2], descrAlt[2],
				true));
		list.add(new ListItem(titles[3], descr[3], titlesAlt[3], descrAlt[3],
				true));
		return list;
	}

	private List<ScheduleListItem> populateScheduleList() {
		ProfileConf ps = new ProfileConf(getActivity());
		Resources res = getResources();
		Date start = new Date(ps.getScheduleStart());
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
		Date stop = new Date(ps.getScheduleStop());
		List<ScheduleListItem> list = new ArrayList<ScheduleListItem>();
		list.add(new ScheduleListItem(res.getString(R.string.enable), res
				.getString(R.string.schedule_start_descr), sdf.format(start),
				ps.isScheduleStartEnabled(), 0));
		list.add(new ScheduleListItem(res.getString(R.string.disable), res
				.getString(R.string.schedule_stop_descr), sdf.format(stop), ps
				.isScheduleStopEnabled(), 1));
		return list;
	}
}
